package com.xfc.web.controller.school;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xfc.common.annotation.Log;
import com.xfc.common.core.controller.BaseController;
import com.xfc.common.core.domain.AjaxResult;
import com.xfc.common.core.page.TableDataInfo;
import com.xfc.common.enums.BusinessType;
import com.xfc.common.utils.poi.ExcelUtil;
import com.xfc.school.domain.ScCourseSchedule;
import com.xfc.school.domain.ScTeacher;
import com.xfc.school.service.IScCourseScheduleService;
import com.xfc.school.service.IScTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 老师信息Controller
 * 
 * @author lcj
 * @date 2021-12-05
 */
@Api(value = "teacher", tags = "老师信息接口")
@RestController
@RequestMapping("/school/teacher")
public class ScTeacherController extends BaseController
{
    @Autowired
    private IScTeacherService scTeacherService;

    @Autowired
    private IScCourseScheduleService courseScheduleService;

    /**
     * 查询老师信息列表
     */
    @ApiOperation("查询老师信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="teacherName",value="老师姓名",dataTypeClass = String.class),
            @ApiImplicitParam(name="teacherMobile",value="老师手机",dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('school:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScTeacher scTeacher){
        startPage();
        List<ScTeacher> list = scTeacherService.list(Wrappers.query(scTeacher));
        return getDataTable(list);
    }

    /**
     * 导出老师信息列表
     */
    @PreAuthorize("@ss.hasPermi('school:teacher:export')")
    @Log(title = "老师信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScTeacher scTeacher)
    {
        List<ScTeacher> list = scTeacherService.list();
        ExcelUtil<ScTeacher> util = new ExcelUtil<ScTeacher>(ScTeacher.class);
        return util.exportExcel(list, "teacher");
    }

    /**
     * 获取老师信息详细信息
     */
    @ApiOperation("获取老师信息详细信息")
    @PreAuthorize("@ss.hasPermi('school:teacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(scTeacherService.getById(id));
    }

    /**
     * 新增老师信息
     */
    @ApiOperation("新增老师信息")
    @PreAuthorize("@ss.hasPermi('school:teacher:add')")
    @Log(title = "老师信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScTeacher scTeacher)
    {
        //新增老师编号
        QueryWrapper<ScTeacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("teacher_number");
        queryWrapper.last("limit 1");
        ScTeacher scTeacher1=scTeacherService.getOne(queryWrapper);
        if (Objects.nonNull(scTeacher1)){
            scTeacher.setTeacherNumber(scTeacher1.getTeacherNumber()+1);
        }else {
            scTeacher.setTeacherNumber(1000);
        }
        //创建时间
        scTeacher.setCreateTime(new Date());
        return toAjax(scTeacherService.save(scTeacher));
    }

    /**
     * 修改老师信息
     */
    @ApiOperation("修改老师信息")
    @PreAuthorize("@ss.hasPermi('school:teacher:edit')")
    @Log(title = "老师信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScTeacher scTeacher)
    {
        //修改时间
        scTeacher.setUpdateTime(new Date());
        return toAjax(scTeacherService.updateById(scTeacher));
    }

    /**
     * 删除老师信息
     */
    @ApiOperation("删除老师信息")
    @PreAuthorize("@ss.hasPermi('school:teacher:remove')")
    @Log(title = "老师信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids){
    	if (ids!=null&&ids.length>0) {
            for (String id : ids) {
                //根据id查询老师是否有课程
                Integer count=courseScheduleService.count(Wrappers.<ScCourseSchedule>lambdaQuery().eq(ScCourseSchedule::getTeacherNumber,id));
                if (count>0){
                    return AjaxResult.error("老师有课程记录,不能删除");
                }
                scTeacherService.removeById(id);
            }
		}else {
            return AjaxResult.error("删除老师不能为空");
        }
        return AjaxResult.success("删除成功");
    }


    /**
     * 根据老师名称模糊查询老师信息
     */
    @ApiOperation("根据老师名称模糊查询老师信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="teacherName",value="老师姓名",dataTypeClass = String.class),
    })
    @GetMapping("/getTeacherByName")
    public AjaxResult getTeacherByName( String teacherName){
        QueryWrapper<ScTeacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","teacher_name");
        queryWrapper.eq("state",0);
        queryWrapper.like(StringUtils.isNotEmpty(teacherName),"teacher_name",teacherName);
        List<ScTeacher> list = scTeacherService.list(queryWrapper);
        return AjaxResult.success(list);
    }


    /**
     * 查询所有老师信息
     */
    @ApiOperation("查询所有老师信息")
    @GetMapping("/getAllTeacher")
    public AjaxResult getAllTeacher(){
        List<ScTeacher> list = scTeacherService.list();
        return AjaxResult.success(list);
    }
}
